package net.towee.client.com.app.presenter;

import net.towee.client.ToweeGinjector;
import net.towee.client.com.app.AppController.Component;
import net.towee.client.com.app.TopPanel;
import net.towee.client.event.core.ShowComponentEvent;
import net.towee.client.event.core.ShowComponentEventHandler;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;

/**
 * Implementation of top panel interface
 * @author c58
 */
public class TopPanelPresenter implements TopPanel, ShowComponentEventHandler, ClickHandler {

	public interface Display extends IsWidget {
		/**
		 * Select some component button
		 * @param com
		 */
		void selectComponent(Component com);
		
		/** 
		 *  Buttons for handle clicking
		 */
		HasClickHandlers getMeButton();
		HasClickHandlers getStuffButton();
		HasClickHandlers getDashBoardButton();
		HasClickHandlers getRepoButton();
		
		void showMainButtons();
		
		void showLobbyButton();
	}
	
	// injected
	private final Display display;
	private final HandlerManager eventBus;
	private final ToweeGinjector injector;
	private Boolean mainButtonsShowed;
	
	@Inject
	public TopPanelPresenter(HandlerManager eventBus, Display display, ToweeGinjector injector) {
		this.display = display;
		this.eventBus = eventBus;
		this.injector = injector;
		
		// On show some component
		eventBus.addHandler(ShowComponentEvent.TYPE, this);
		
		// On click to some button
		display.getMeButton().addClickHandler(this);
		display.getStuffButton().addClickHandler(this);
		display.getDashBoardButton().addClickHandler(this);
		display.getRepoButton().addClickHandler(this);
	}
	
	@Override
	public void showMainButtons() {
		mainButtonsShowed = true;
		display.showMainButtons();
	}

	@Override
	public void showLobbyButton() {
		mainButtonsShowed = false;
		display.showLobbyButton();
	}
	
	@Override
	public void go(HasWidgets container) {
		// Add title to container
		container.clear();
		container.add( display.asWidget() );
	}

	@Override
	public void onClick(ClickEvent event) {
		Component com = null;
		
		// Get source component
		if( event.getSource().equals( display.getMeButton() ))
			com = Component.ME;
		else if( event.getSource().equals( display.getStuffButton() ))
			com = Component.STUFF;
		else if( event.getSource().equals( display.getDashBoardButton() ))
			com = Component.DASHBOARD;
		else if( event.getSource().equals( display.getRepoButton() ))
			com = Component.REPO;
		
		// Fire show component event
		if( com != null ) {
			eventBus.fireEvent(new ShowComponentEvent(com));
			display.selectComponent(com);
		}
	}

	@Override
	public void onShowComponent(Component component, String... params) {
		if(component == Component.LOBBY && (mainButtonsShowed == null || mainButtonsShowed == true ))
			showLobbyButton();
		else if(component != Component.LOBBY && (mainButtonsShowed == null || mainButtonsShowed == false ))
			showMainButtons();
		
		display.selectComponent(component);
	}

}
